Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 (api.py): Raise correct error when timeout #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Zerka30
Copy link

@Zerka30 Zerka30 commented Aug 10, 2023

Related to : #44

I'll let you try it for yourself, and let me know if this function has any dependencies elsewhere.

@Zerka30 Zerka30 force-pushed the return-timeout branch 3 times, most recently from eea3c48 to 3c9dece Compare August 10, 2023 10:10
@lucasheld
Copy link
Owner

I think that is not quite correct.
The Uptime Kuma errors are in r.get("msg"). Now they are no longer intercepted, because an Expection is not thrown in this case.

I would do it like this:

def _call(self, event, data=None) -> Any:
    try:
        r = self.sio.call(event, data, timeout=self.timeout)
    except socketio.exceptions.TimeoutError:
        raise Timeout(f"Timed out while waiting for event {event}")
    if isinstance(r, dict) and "ok" in r:
        if not r["ok"]:
            raise UptimeKumaException(r.get("msg"))
        r.pop("ok")
    return r

@Zerka30
Copy link
Author

Zerka30 commented Aug 16, 2023

Hum 🤔 , wdym exactly?

Because the way I've done it, it will return the correct error if socket.io returns a TimeoutError, and for any other type of error it will return an UptimeKumaException with the error message contained in r.get("msg")

@Zerka30
Copy link
Author

Zerka30 commented Aug 25, 2023

@lucasheld , I ping you, to correct if necessary

if not r["ok"]:
raise UptimeKumaException(r.get("msg"))
try:
r = self.sio.call(event, data, timeout=self.timeout)
Copy link
Owner

@lucasheld lucasheld Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The response of self.sio.call is not always a dict (for example api.need_setup). In this case r.pop("ok") fails. You have deleted the code that checks if the response type is a dict.

Also you have deleted the code that checks the r["ok"] value. The UptimeKumaException is now no longer raised when Uptime Kuma responds with an error message (when r["ok"] is false).

except socketio.exceptions.TimeoutError:
raise Timeout(f"Timed out while waiting for event {event}")
except Exception as e:
raise UptimeKumaException(r.get("msg"))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r is not defined when self.sio.call fails.

@lucasheld
Copy link
Owner

I think that is not quite correct. The Uptime Kuma errors are in r.get("msg"). Now they are no longer intercepted, because an Expection is not thrown in this case.

I would do it like this:

def _call(self, event, data=None) -> Any:
    try:
        r = self.sio.call(event, data, timeout=self.timeout)
    except socketio.exceptions.TimeoutError:
        raise Timeout(f"Timed out while waiting for event {event}")
    if isinstance(r, dict) and "ok" in r:
        if not r["ok"]:
            raise UptimeKumaException(r.get("msg"))
        r.pop("ok")
    return r

I have tested this code. It works as expected.

The only problem I noticed is that there are still exceptions directly from socketio (for example socketio.exceptions.ConnectionError).
Then these Excepetions should either be caught too or we have to use separate timeout exceptions, one for the logic within uptime-kuma-api and one for socketio timeouts. Currently, I prefer the latter.

@Zerka30
Copy link
Author

Zerka30 commented Aug 31, 2023

Alright, I understand and now its fix ! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants